// 提交链接 https://leetcode.cn/problems/koko-eating-bananas/submissions/565401136/
// 875. 爱吃香蕉的珂珂
// 完成日期： 2024.9.17
// 二分搜索
class Solution {
public:
    int minEatingSpeed(vector<int>& piles, int h) {
        int head = 1, tail = -1;
        for (auto& x : piles) tail = max(tail, x);
        while (head < tail)
        {
            int mid = (head + tail) / 2;
            int num = h;
            for (int i = 0; i < piles.size(); i++)
            {
                num -= piles[i] / mid + (piles[i] % mid != 0);
                if (num < 0) break;
            }
            if (num < 0) head = mid + 1;
            else tail = mid;
        }
        return head;
    }
};